﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ShengYueWorkShop.TcpClient
{
    public partial class SqlView : Form
    {
        /// <summary>
        /// 数据库帮助类
        /// </summary>
        public BLL.PublicHelper.SqlHelper _SqlHelper { get; set; } = new BLL.PublicHelper.SqlHelper();
        public SqlView()
        {
            InitializeComponent();
            this.Shown += SqlView_Shown;
        }

        private void SqlView_Shown(object sender, EventArgs e)
        {
            //关联事件
            KJ_TestBtn.Click += KJ_TestBtn_Click;
        }

        private void KJ_TestBtn_Click(object sender, EventArgs e)
        {
            #region 判定条件
            if( KJ_Server.Text.Length==0 || 
                KJ_Port.Text.Length==0 || 
                KJ_User.Text.Length==0 || 
                KJ_Pwd.Text.Length==0)
            {
                MessageBox.Show("请输入数据");
            }
            #endregion

            #region 获取连接字符串
            //server=127.0.0.1;database=DATABASE;uid=sa;pwd=
            StringBuilder fStr = new StringBuilder();
            fStr.AppendFormat("server={0}", KJ_Server.Text.Trim());
            int fPort = 1433;
            if(int.TryParse(KJ_Port.Text,out fPort))
            {
                if(fPort!=1433)
                {
                    fStr.AppendFormat(":{0}", fPort);
                }
            }

            if (KJ_DbName.Text.Length == 0)
            {
                fStr.AppendFormat(";database={0};", "Master");
            }
            else
            {
                fStr.AppendFormat(";database={0};", KJ_DbName.Text.Trim());
            }
            fStr.AppendFormat("uid={0};pwd={1}", KJ_User.Text.Trim(), KJ_Pwd.Text.Trim());
            #endregion

            #region 测试或保存字符串到appconfig文件中
            if ("测试" == KJ_TestBtn.Text)
            {
                List<string> fList = new List<string>();
                fList = _SqlHelper.TestDbLink(fStr.ToString());
                if(fList.Count>0)
                {
                    KJ_TestBtn.Text = "保存";
                    foreach (string fDbName in fList)
                    {
                        KJ_DbName.Items.Add(fDbName);
                    }
                    MessageBox.Show("连接成功");
                }
                else
                {
                    MessageBox.Show("连接失败");
                }
            }
            else if ("保存" == KJ_TestBtn.Text)
            {
                if (KJ_DbName.SelectedIndex >= 0)
                {
                    if (_SqlHelper.SaveSqlConnForAppConfig(fStr.ToString()))
                    {
                        MessageBox.Show("保存成功，将关闭该窗口");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("保存失败，请联系管理员");
                    }
                }
                else
                {
                    MessageBox.Show("请选择数据库");
                }
            }
            #endregion
        }
    }
}
